from PyQt5.QtCore import *
from examples.example_calculator.calc_conf import *
from examples.example_calculator.calc_node_base import *
from nodeeditor.utils import dumpException


class CalcYiCiContent(QDMNodeContentWidget):
    def initUI(self):
        self.layout = QHBoxLayout()
        self.layout.setContentsMargins(10,0,10,10)
        self.setLayout(self.layout)
        
        self.edit = QLineEdit("0000", self)
        self.edit.setAlignment(Qt.AlignLeft)
        self.edit.setObjectName(self.node.content_label_objname)
        
        self.edit1 = QLineEdit("0", self)
        self.edit1.setAlignment(Qt.AlignLeft)
        self.edit1.setObjectName(self.node.content_label_objname)
        
        self.layout.addWidget(self.edit)
        self.layout.addWidget(self.edit1)
    
    def serialize(self):
        res = super().serialize()
        res['value'] = self.edit.text()
        res['value1'] = self.edit1.text()
        return res

    def deserialize(self, data, hashmap={}):
        res = super().deserialize(data, hashmap)
        try:
            value = data['value']
            self.edit.setText(value)
            value1 = data['value1']
            self.edit1.setText(value1)
            return True & res
        except Exception as e:
            dumpException(e)
        return res


@register_node(OP_NODE_ONE)
class CalcNode_One(CalcNode):
    icon = "icons/add.png"
    op_code = OP_NODE_ONE
    op_title = "只执行一次"
    content_label = ""
    content_label_objname = "calc_node_one"
    
    def __init__(self, scene):
        super().__init__(scene, inputs=[1], outputs=[1])
        self.eval()
    
    def initInnerClasses(self):
        self.content = CalcYiCiContent(self)
        self.grNode = CalcGraphicsNode(self)
        self.content.edit.textChanged.connect(self.onInputChanged)
        self.content.edit1.textChanged.connect(self.onInputChanged)
    
    def evalImplementation(self):
        s_value = self.getInput(0).eval()
        self.value = self.setMyText(s_value)
            
            
        self.markDirty(False)
        self.markInvalid(False)

        self.markDescendantsInvalid(False)
        self.markDescendantsDirty()

        self.grNode.setToolTip("")

        self.evalChildren()

        return self.value
    
    def setMyText(self,str01):
        #设置输入的文本，让其拥有只执行一次的功能
        #str01---输入的文本
        str01 = '    ' + str01.replace('\n','\n    ')#给每一行加上4个空格
        
        str02 = self.content.edit.text()
        str03 = self.content.edit1.text()
        str01 = 'if(Logs("get","' + str02 + '","执行一次' + str03 + '") != "真"):\n' + str01
        
        str01 = str01 + '\n    Logs("set","' + str02 + '","执行一次' + str03 + '","真")' + '\n'
        
        return str01